package com.whu.waterworks.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.whu.waterworks.dao.GisStationHistoryDataDao;
import com.whu.waterworks.model.Historydata;
import com.whu.waterworks.model.Realtimedata;
import com.whu.waterworks.model.RealtimedataId;

public class GisStationHistoryDataService {
	private GisStationHistoryDataDao gisStationHistoryDataDao;
	public List<Realtimedata> getDataByTwoTimeLimitTenRecords(String id,String type,String beginTime,String endTime)
	{
		List<Realtimedata> resutDatas = new ArrayList<>();
		String begin = beginTime+"/"+"00:00:00";
		String end = endTime +"/"+"24:00:00";
		//SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		/*Date endDate = format.parse(endTime);
		if(dateUtil.isToday(endDate))//结束时间为今天，则涉及两个历史表和实时表
		{
			List<Realtimedata> realtimedatas = realtimedataDAO.findByIdTodayAll(id, type);
			if(realtimedatas!=null && realtimedatas.size()>0)
			{
				System.out.println("Data Service realtimedatas.size()="+realtimedatas.size());
				resutDatas.addAll(realtimedatas);
			}
			List<Historydata> historydatas = historydataDAO.findByIdAllTime(id, type, begin);
			//如果有历史数据，则将历史数据加入
			if(historydatas!=null && historydatas.size()>0)
			{
				System.out.println("Data Service historydatas.size()="+historydatas.size());
				for(Historydata historydata:historydatas)
				{
					Realtimedata temp = new Realtimedata();
					RealtimedataId tId = new RealtimedataId();
					
					try {
						PropertyUtils.copyProperties(tId,historydata.getId());
					} catch (IllegalAccessException | InvocationTargetException
							| NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					temp.setId(tId);
					temp.setValue(historydata.getValue());
					resutDatas.add(temp);
				}
			}
		}
		else*/ {//只涉及历史表
			List<Historydata> historydatas = gisStationHistoryDataDao.findByIdInTimetenRecords(id, type, end, begin);
			//如果有历史数据，则将历史数据加入
			if(historydatas!=null && historydatas.size()>0)
			{
				for(Historydata historydata:historydatas)
				{
					Realtimedata temp = new Realtimedata();
					RealtimedataId tId = new RealtimedataId();
					
					try {
						PropertyUtils.copyProperties(tId,historydata.getId());
					} catch (IllegalAccessException | InvocationTargetException
							| NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					temp.setId(tId);
					temp.setValue(historydata.getValue());
					resutDatas.add(temp);
				}
			}
		}	
		return resutDatas;
	}
	public GisStationHistoryDataDao getGisStationHistoryDataDao() {
		return gisStationHistoryDataDao;
	}
	public void setGisStationHistoryDataDao(
			GisStationHistoryDataDao gisStationHistoryDataDao) {
		this.gisStationHistoryDataDao = gisStationHistoryDataDao;
	}
	
}
